package cn.edu.szu;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Random;


public class Main {
    // 随机数生成器
    private Random random;

    // 共享文本输出框
    private JTextArea area;

    public static void main(String[] args) {
        new Main().test();
    }

    private void test()  {
        // 初始化界面
        createAndShowGUI();
        
        // 初始化对象
        init();
    }

    private void createAndShowGUI() {

        // 创建及设置窗口
        JFrame frame = new JFrame("读写者问题");
        frame.setSize(600, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 设置菜单栏
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        // 菜单栏添加菜单
        JMenu menu = new JMenu("线程同步");
        menuBar.add(menu);

        JMenuItem item1 = new JMenuItem(new AbstractAction("五写者和五读者") {
            @Override
            public void actionPerformed(ActionEvent e) {
                area.setText("");
                first();
            }
        });

        JMenuItem item2 = new JMenuItem(new AbstractAction("一写者和五读者") {
            @Override
            public void actionPerformed(ActionEvent e) {
                area.setText("");
                second();
            }
        });

        JMenuItem item3 = new JMenuItem(new AbstractAction("随机读者和写者") {
            @Override
            public void actionPerformed(ActionEvent e) {
                area.setText("");
                third();
            }
        });

        menu.add(item1);
        menu.add(item2);
        menu.add(item3);

        area = new JTextArea();
        area.setFont(new Font("Consolas",0,20));
        JScrollPane pane = new JScrollPane(area);

        frame.setContentPane(pane);

        // 显示窗口
        frame.setVisible(true);
    }

    // 第1种情况，5个写者线程和5个读者线程
    private void first() {
        area.append("5 writers and 5 readers:" + "\n\n");
        myThread.clearFile();
        for (int i=0; i<5; ++i) {
            new Thread(new Writer(random.nextInt(100))).start();
        }

        for (int i=0; i<5; ++i) {
            new Thread(new Reader(random.nextInt(100))).start();
        }
    }

    // 第2种情况，1个写者线程和5个读者线程
    private void second() {
        area.append("1 writers and 5 readers:" + "\n\n");
        myThread.clearFile();
        new Thread(new Writer(random.nextInt(100))).start();

        for (int i=0; i<5; ++i) {
            new Thread(new Reader(random.nextInt(100))).start();
        }
    }

    // 第3种情况，随机创建写者线程和读者线程，保证读者更多
    private void third() {
        area.append("random writers and random readers:" + "\n\n");
        myThread.clearFile();

        int write_count = random.nextInt(20), read_count = random.nextInt(20);
        if (read_count < write_count) {
            int tmp = read_count;
            read_count = write_count;
            write_count = tmp;
        }

        for (int i=0; i<write_count; ++i) {
            new Thread(new Writer(random.nextInt(100))).start();
        }

        for (int i=0; i<read_count; ++i) {
            new Thread(new Reader(random.nextInt(100))).start();
        }
    }

    private void init() {
        random = new Random();
        // 共享变量初始化
        myThread.init(area);
    }
}
